<template>
  <div class="profile-container">
    <!-- 顶部个人信息卡片 -->
    <div class="profile-header-card">
      <div class="profile-background">
        <div class="background-gradient"></div>
        <div class="background-pattern"></div>
      </div>
      
      <div class="profile-header-content">
        <div class="avatar-section">
          <div class="avatar-wrapper">
            <img :src="userProfile.avatar" :alt="userProfile.name" class="user-avatar" />
            <div class="avatar-edit-badge">
              <el-icon><Camera /></el-icon>
            </div>
          </div>
        </div>
        
        <div class="user-info-section">
          <div class="user-name-section">
            <h1 class="user-name">{{ userProfile.name }}</h1>
            <div class="user-badge">
              <el-tag size="small" effect="light" :type="userProfile.role === 'admin' ? 'primary' : 'success'">
                {{ userProfile.role === 'admin' ? '管理员' : '普通用户' }}
              </el-tag>
            </div>
          </div>
          
          <div class="user-meta">
            <div class="meta-item">
              <el-icon class="meta-icon"><Position /></el-icon>
              <span>{{ userProfile.department }}</span>
            </div>
            <div class="meta-item">
              <el-icon class="meta-icon"><Phone /></el-icon>
              <span>{{ userProfile.phone }}</span>
            </div>
            <div class="meta-item">
              <el-icon class="meta-icon"><Message /></el-icon>
              <span>{{ userProfile.email }}</span>
            </div>
          </div>
          
          <div class="user-stats">
            <div class="stat-item">
              <div class="stat-value">{{ userProfile.stats.completedTasks }}</div>
              <div class="stat-label">完成任务</div>
            </div>
            <div class="stat-item">
              <div class="stat-value">{{ userProfile.stats.followedCustomers }}</div>
              <div class="stat-label">跟进客户</div>
            </div>
            <div class="stat-item">
              <div class="stat-value">{{ userProfile.stats.createdOpportunities }}</div>
              <div class="stat-label">创建商机</div>
            </div>
            <div class="stat-item">
              <div class="stat-value">{{ userProfile.stats.completedDeals }}</div>
              <div class="stat-label">成交数量</div>
            </div>
          </div>
        </div>
        
        <div class="profile-actions">
          <el-button type="primary" class="edit-profile-btn">
            <el-icon><EditPen /></el-icon>
            编辑资料
          </el-button>
          <el-button class="settings-btn">
            <el-icon><Setting /></el-icon>
            账号设置
          </el-button>
        </div>
      </div>
    </div>

    <!-- 内容区域 -->
    <div class="profile-content">
      <!-- 左侧导航 -->
      <div class="profile-sidebar">
        <el-menu
          :default-active="activeTab"
          class="profile-menu"
          @select="handleMenuSelect"
          background-color="#f5f7fa"
          text-color="#606266"
          active-text-color="#409eff"
        >
          <el-menu-item index="overview">
            <el-icon><DataAnalysis /></el-icon>
            <span>工作概览</span>
          </el-menu-item>
          <el-menu-item index="performance">
            <el-icon><TrendCharts /></el-icon>
            <span>业绩分析</span>
          </el-menu-item>
          <el-menu-item index="recent-activities">
            <el-icon><Timer /></el-icon>
            <span>近期活动</span>
          </el-menu-item>
          <el-menu-item index="tasks">
            <el-icon><DocumentChecked /></el-icon>
            <span>我的任务</span>
          </el-menu-item>
          <el-menu-item index="notifications">
            <el-icon><Bell /></el-icon>
            <span>通知中心</span>
          </el-menu-item>
          <el-menu-item index="preferences">
            <el-icon><User /></el-icon>
            <span>个人偏好</span>
          </el-menu-item>
        </el-menu>
      </div>

      <!-- 右侧内容 -->
      <div class="profile-main">
        <!-- 工作概览 -->
        <div v-if="activeTab === 'overview'" class="overview-section">
          <div class="section-header">
            <h2>工作概览</h2>
            <div class="time-range-selector">
              <el-radio-group v-model="selectedRange" size="small">
                <el-radio-button label="week">本周</el-radio-button>
                <el-radio-button label="month">本月</el-radio-button>
                <el-radio-button label="quarter">本季度</el-radio-button>
                <el-radio-button label="year">本年</el-radio-button>
              </el-radio-group>
            </div>
          </div>

          <!-- 业绩卡片 -->
          <div class="performance-cards">
            <el-card class="performance-card">
              <div class="card-content">
                <div class="card-icon primary">
                  <el-icon><TrendCharts /></el-icon>
                </div>
                <div class="card-info">
                  <div class="card-value">¥{{ overviewData.revenue.toLocaleString() }}</div>
                  <div class="card-label">销售业绩</div>
                  <div class="card-change" :class="overviewData.revenueChange >= 0 ? 'positive' : 'negative'">
                    <el-icon v-if="overviewData.revenueChange >= 0"><ArrowUp /></el-icon>
                    <el-icon v-else><ArrowDown /></el-icon>
                    {{ Math.abs(overviewData.revenueChange) }}%
                  </div>
                </div>
              </div>
            </el-card>

            <el-card class="performance-card">
              <div class="card-content">
                <div class="card-icon success">
                  <el-icon><Check /></el-icon>
                </div>
                <div class="card-info">
                  <div class="card-value">{{ overviewData.completedTasks }}</div>
                  <div class="card-label">完成任务</div>
                  <div class="card-change" :class="overviewData.taskCompletionRate >= 80 ? 'positive' : 'negative'">
                    完成率 {{ overviewData.taskCompletionRate }}%
                  </div>
                </div>
              </div>
            </el-card>

            <el-card class="performance-card">
              <div class="card-content">
                <div class="card-icon warning">
                  <el-icon><Timer /></el-icon>
                </div>
                <div class="card-info">
                  <div class="card-value">{{ overviewData.avgResponseTime }}h</div>
                  <div class="card-label">平均响应时间</div>
                  <div class="card-change" :class="overviewData.avgResponseTime <= 24 ? 'positive' : 'negative'">
                    {{ overviewData.avgResponseTime <= 24 ? '良好' : '需改进' }}
                  </div>
                </div>
              </div>
            </el-card>

            <el-card class="performance-card">
              <div class="card-content">
                <div class="card-icon danger">
                  <el-icon><Star /></el-icon>
                </div>
                <div class="card-info">
                  <div class="card-value">{{ overviewData.customerSatisfaction }}%</div>
                  <div class="card-label">客户满意度</div>
                  <div class="card-change" :class="overviewData.customerSatisfaction >= 90 ? 'positive' : 'negative'">
                    {{ overviewData.customerSatisfaction >= 90 ? '优秀' : '需提升' }}
                  </div>
                </div>
              </div>
            </el-card>
          </div>

          <!-- 图表区域 -->
          <div class="charts-section">
            <el-card class="chart-card">
              <template #header>
                <div class="chart-header">
                  <span>业绩趋势</span>
                  <el-select v-model="chartType" size="small" placeholder="选择图表类型">
                    <el-option label="折线图" value="line" />
                    <el-option label="柱状图" value="bar" />
                  </el-select>
                </div>
              </template>
              <div class="chart-placeholder">
                <div class="chart-loading">
                  <el-icon class="loading-icon"><Loading /></el-icon>
                  <span>加载业绩图表...</span>
                </div>
              </div>
            </el-card>

            <el-card class="chart-card">
              <template #header>
                <div class="chart-header">
                  <span>工作分布</span>
                  <el-select v-model="distributionType" size="small" placeholder="分布类型">
                    <el-option label="按任务类型" value="task" />
                    <el-option label="按客户行业" value="industry" />
                  </el-select>
                </div>
              </template>
              <div class="chart-placeholder">
                <div class="chart-loading">
                  <el-icon class="loading-icon"><Loading /></el-icon>
                  <span>加载分布图表...</span>
                </div>
              </div>
            </el-card>
          </div>

          <!-- 待办任务 -->
          <div class="tasks-section">
            <el-card>
              <template #header>
                <div class="section-header-inner">
                  <span>近期任务</span>
                  <el-button type="text" size="small">查看全部</el-button>
                </div>
              </template>
              <div class="tasks-list">
                <div v-for="task in recentTasks" :key="task.id" class="task-item">
                  <div class="task-checkbox">
                    <el-checkbox v-model="task.completed" />
                  </div>
                  <div class="task-content">
                    <div class="task-title">{{ task.title }}</div>
                    <div class="task-meta">
                      <span class="task-deadline">截止日期: {{ task.deadline }}</span>
                      <el-tag :size="'small'" :type="getTaskPriorityType(task.priority)">
                        {{ task.priority }}
                      </el-tag>
                    </div>
                  </div>
                  <div class="task-actions">
                    <el-button type="text" size="small">处理</el-button>
                  </div>
                </div>
              </div>
            </el-card>
          </div>
        </div>

        <!-- 其他标签页内容占位 -->
        <div v-else class="tab-placeholder">
          <el-empty description="该功能模块正在开发中..." />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import {
  User, Camera, EditPen, Setting, Position, Phone, Message,
  DataAnalysis, TrendCharts, Timer, DocumentChecked, Bell,
  ArrowUp, ArrowDown, Check, Star, Loading
} from '@element-plus/icons-vue'
import { getUserInfo } from '@/api/user'

// 当前激活的标签
const activeTab = ref('overview')
// 时间范围选择
const selectedRange = ref('month')
// 图表类型
const chartType = ref('line')
const distributionType = ref('task')

// 用户个人信息
const userProfile = reactive({
  name: '管理员',
  role: 'admin',
  department: '技术部',
  phone: '13800138000',
  email: 'admin@example.com',
  avatar: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png',
  stats: {
    completedTasks: 156,
    followedCustomers: 48,
    createdOpportunities: 23,
    completedDeals: 12
  }
})

// 概览数据
const overviewData = reactive({
  revenue: 1258000,
  revenueChange: 12.5,
  completedTasks: 48,
  taskCompletionRate: 85,
  avgResponseTime: 18,
  customerSatisfaction: 92
})

// 近期任务
const recentTasks = reactive([
  {
    id: 1,
    title: '完成Q4季度报告',
    deadline: '2024-12-31',
    priority: '高',
    completed: false
  },
  {
    id: 2,
    title: '跟进重要客户需求',
    deadline: '2024-12-15',
    priority: '紧急',
    completed: false
  },
  {
    id: 3,
    title: '团队周会准备',
    deadline: '2024-12-10',
    priority: '中',
    completed: true
  },
  {
    id: 4,
    title: '系统功能测试',
    deadline: '2024-12-20',
    priority: '中',
    completed: false
  },
  {
    id: 5,
    title: '更新产品文档',
    deadline: '2024-12-25',
    priority: '低',
    completed: false
  }
])

// 获取任务优先级对应的类型
const getTaskPriorityType = (priority) => {
  const typeMap = {
    '紧急': 'danger',
    '高': 'warning',
    '中': 'primary',
    '低': 'success'
  }
  return typeMap[priority] || 'info'
}

// 处理菜单选择
const handleMenuSelect = (key) => {
  activeTab.value = key
}

// 加载用户信息
const loadUserInfo = async () => {
  try {
    const response = await getUserInfo()
    if (response.data) {
      // 更新用户信息
      if (response.data.name) userProfile.name = response.data.name
      if (response.data.username) userProfile.name = response.data.username
      if (response.data.vcLoginname) userProfile.name = response.data.vcLoginname
      if (response.data.role) userProfile.role = response.data.role
      if (response.data.department) userProfile.department = response.data.department
      if (response.data.phone) userProfile.phone = response.data.phone
      if (response.data.email) userProfile.email = response.data.email
      if (response.data.image) userProfile.avatar = response.data.image
    }
  } catch (error) {
    console.error('获取用户信息失败:', error)
  }
}

// 组件挂载时加载数据
onMounted(() => {
  loadUserInfo()
})
</script>

<style scoped>
.profile-container {
  min-height: 100vh;
  background-color: #f0f2f5;
}

/* 顶部个人信息卡片 */
.profile-header-card {
  position: relative;
  margin-bottom: 30px;
  overflow: hidden;
}

.profile-background {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 200px;
  z-index: 0;
}

.background-gradient {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 100%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.background-pattern {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 100%;
  background-image: url("data:image/svg+xml,%3Csvg width='100' height='100' viewBox='0 0 100 100' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M11 18c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm48 25c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm-43-7c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm63 31c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM34 90c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm56-76c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM12 86c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm28-65c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm23-11c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-6 60c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm29 22c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zM32 63c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm57-13c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-9-21c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM60 91c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM35 41c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM12 60c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2z' fill='%23ffffff' fill-opacity='0.1' fill-rule='evenodd'/%3E%3C/svg%3E");
}

.profile-header-content {
  position: relative;
  z-index: 1;
  padding: 20px 30px;
  display: flex;
  align-items: flex-start;
  gap: 30px;
}

.avatar-section {
  margin-top: -80px;
}

.avatar-wrapper {
  position: relative;
  width: 160px;
  height: 160px;
  border-radius: 50%;
  overflow: hidden;
  border: 5px solid white;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  background-color: white;
}

.user-avatar {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.avatar-edit-badge {
  position: absolute;
  bottom: 10px;
  right: 10px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background-color: #409eff;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
}

.avatar-edit-badge:hover {
  background-color: #66b1ff;
  transform: scale(1.1);
}

.user-info-section {
  flex: 1;
  padding-top: 30px;
}

.user-name-section {
  display: flex;
  align-items: center;
  gap: 15px;
  margin-bottom: 15px;
}

.user-name {
  font-size: 32px;
  font-weight: 700;
  color: #303133;
  margin: 0;
}

.user-meta {
  display: flex;
  gap: 30px;
  margin-bottom: 25px;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #606266;
  font-size: 14px;
}

.meta-icon {
  color: #409eff;
}

.user-stats {
  display: flex;
  gap: 40px;
  margin-bottom: 30px;
}

.stat-item {
  text-align: center;
}

.stat-value {
  font-size: 28px;
  font-weight: 700;
  color: #303133;
  line-height: 1;
}

.stat-label {
  font-size: 14px;
  color: #606266;
  margin-top: 5px;
}

.profile-actions {
  display: flex;
  gap: 15px;
}

.edit-profile-btn,
.settings-btn {
  padding: 10px 20px;
  border-radius: 6px;
  transition: all 0.3s ease;
}

.edit-profile-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

.settings-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

/* 内容区域 */
.profile-content {
  display: flex;
  gap: 20px;
  padding: 0 30px;
}

.profile-sidebar {
  width: 240px;
  flex-shrink: 0;
}

.profile-menu {
  border-radius: 8px;
  overflow: hidden;
}

.profile-main {
  flex: 1;
}

/* 概览区域 */
.overview-section {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.section-header h2 {
  font-size: 24px;
  font-weight: 600;
  color: #303133;
  margin: 0;
}

.time-range-selector {
  background-color: white;
  padding: 5px;
  border-radius: 6px;
}

/* 业绩卡片 */
.performance-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 20px;
}

.performance-card {
  border-radius: 12px;
  overflow: hidden;
  transition: all 0.3s ease;
}

.performance-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
}

.card-content {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 20px;
}

.card-icon {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  color: white;
}

.card-icon.primary {
  background-color: #409eff;
}

.card-icon.success {
  background-color: #67c23a;
}

.card-icon.warning {
  background-color: #e6a23c;
}

.card-icon.danger {
  background-color: #f56c6c;
}

.card-info {
  flex: 1;
}

.card-value {
  font-size: 24px;
  font-weight: 700;
  color: #303133;
  margin-bottom: 5px;
}

.card-label {
  font-size: 14px;
  color: #606266;
  margin-bottom: 5px;
}

.card-change {
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 3px;
}

.card-change.positive {
  color: #67c23a;
}

.card-change.negative {
  color: #f56c6c;
}

/* 图表区域 */
.charts-section {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 20px;
}

.chart-card {
  border-radius: 12px;
  overflow: hidden;
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.chart-placeholder {
  height: 300px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #fafafa;
  border-radius: 8px;
  margin-top: 10px;
}

.chart-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  color: #909399;
}

.loading-icon {
  font-size: 32px;
  animation: rotate 2s linear infinite;
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

/* 任务列表 */
.tasks-section {
  margin-top: 10px;
}

.section-header-inner {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.tasks-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.task-item {
  display: flex;
  align-items: center;
  gap: 15px;
  padding: 15px;
  background-color: #fafafa;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.task-item:hover {
  background-color: #f0f9ff;
  transform: translateX(5px);
}

.task-checkbox {
  flex-shrink: 0;
}

.task-content {
  flex: 1;
}

.task-title {
  font-size: 16px;
  font-weight: 500;
  color: #303133;
  margin-bottom: 5px;
}

.task-meta {
  display: flex;
  align-items: center;
  gap: 15px;
}

.task-deadline {
  font-size: 14px;
  color: #909399;
}

/* 标签页占位 */
.tab-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 400px;
  background-color: white;
  border-radius: 12px;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .charts-section {
    grid-template-columns: 1fr;
  }
  
  .user-stats {
    flex-wrap: wrap;
    gap: 20px;
  }
}

@media (max-width: 768px) {
  .profile-header-content {
    flex-direction: column;
    align-items: center;
    text-align: center;
  }
  
  .user-info-section {
    padding-top: 0;
  }
  
  .user-name-section {
    flex-direction: column;
    gap: 10px;
  }
  
  .user-meta {
    justify-content: center;
    flex-wrap: wrap;
    gap: 15px;
  }
  
  .profile-actions {
    justify-content: center;
  }
  
  .profile-content {
    flex-direction: column;
  }
  
  .profile-sidebar {
    width: 100%;
  }
  
  .performance-cards {
    grid-template-columns: 1fr;
  }
  
  .section-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }
}
</style>